using System;
using System.Data.Entity.Migrations;

namespace Altairis.FluffyCloud.Data.Migrations {

    public partial class Initial : DbMigration {

        public override void Up() {
            CreateTable(
                "dbo.Roles",
                c => new {
                    RoleName = c.String(nullable: false, maxLength: 100),
                    Description = c.String(maxLength: 50),
                })
                .PrimaryKey(t => t.RoleName);

            CreateTable(
                "dbo.Users",
                c => new {
                    UserName = c.String(nullable: false, maxLength: 100),
                    PasswordHash = c.Binary(nullable: false, maxLength: 64),
                    PasswordSalt = c.Binary(nullable: false, maxLength: 128),
                    Email = c.String(nullable: false, maxLength: 100),
                    IsApproved = c.Boolean(nullable: false),
                    DateCreated = c.DateTimeOffset(nullable: false),
                    DateLastLogin = c.DateTimeOffset(),
                    DateLastActivity = c.DateTimeOffset(),
                    DateLastPasswordChange = c.DateTimeOffset(nullable: false),
                    SecurityCode = c.String(nullable: false, maxLength: 43, defaultValue: "___________________________________________"),
                    SendNewMessagesToEmail = c.Boolean(nullable: false, defaultValue: true),
                    Comment = c.String(maxLength: 50),
                    Species = c.String(maxLength: 50),
                    Description = c.String(),
                    FullName = c.String(maxLength: 50),
                    BirthDate = c.DateTime(),
                    City = c.String(maxLength: 50),
                    HomePage = c.String(maxLength: 50),
                    PhoneNumber = c.String(maxLength: 20),
                    Icq = c.String(maxLength: 15),
                    Jabber = c.String(maxLength: 50),
                    Twitter = c.String(maxLength: 16),
                    Facebook = c.String(maxLength: 30),
                    FurNet = c.String(maxLength: 30),
                })
                .PrimaryKey(t => t.UserName);

            CreateTable(
                "dbo.UserAclEntries",
                c => new {
                    AclEntryId = c.Guid(nullable: false),
                    UserName = c.String(nullable: false, maxLength: 100),
                    RoleName = c.String(nullable: false, maxLength: 100),
                    Operation = c.String(nullable: false, maxLength: 20),
                    Action = c.Boolean(nullable: false),
                })
                .PrimaryKey(t => t.AclEntryId)
                .ForeignKey("dbo.Users", t => t.UserName, cascadeDelete: true)
                .ForeignKey("dbo.Roles", t => t.RoleName, cascadeDelete: true)
                .Index(t => t.UserName)
                .Index(t => t.RoleName);

            CreateTable(
                "dbo.Messages",
                c => new {
                    MessageId = c.Guid(nullable: false),
                    FromUserName = c.String(nullable: false, maxLength: 100),
                    ToUserName = c.String(nullable: false, maxLength: 100),
                    DateCreated = c.DateTimeOffset(nullable: false),
                    DateRead = c.DateTimeOffset(),
                    Text = c.String(),
                })
                .PrimaryKey(t => t.MessageId)
                .ForeignKey("dbo.Users", t => t.FromUserName, cascadeDelete: false)
                .ForeignKey("dbo.Users", t => t.ToUserName, cascadeDelete: false)
                .Index(t => t.FromUserName)
                .Index(t => t.ToUserName);

            CreateTable(
                "dbo.TopicCategories",
                c => new {
                    TopicCategoryId = c.Int(nullable: false, identity: true),
                    Name = c.String(nullable: false, maxLength: 100),
                    IsSystemCategory = c.Boolean(nullable: false),
                })
                .PrimaryKey(t => t.TopicCategoryId);

            CreateTable(
                "dbo.Topics",
                c => new {
                    TopicId = c.Int(nullable: false, identity: true),
                    UserName = c.String(nullable: false, maxLength: 100),
                    TopicCategoryId = c.Int(nullable: false),
                    Name = c.String(nullable: false, maxLength: 50),
                    Description = c.String(nullable: false, maxLength: 100),
                    HeaderText = c.String(),
                    PublicHeaderText = c.String(),
                    DateCreated = c.DateTimeOffset(nullable: false),
                })
                .PrimaryKey(t => t.TopicId)
                .ForeignKey("dbo.Users", t => t.UserName, cascadeDelete: true)
                .ForeignKey("dbo.TopicCategories", t => t.TopicCategoryId, cascadeDelete: true)
                .Index(t => t.UserName)
                .Index(t => t.TopicCategoryId);

            CreateTable(
                "dbo.TopicAclEntries",
                c => new {
                    AclEntryId = c.Guid(nullable: false),
                    TopicId = c.Int(nullable: false),
                    RoleName = c.String(nullable: false, maxLength: 100),
                    Operation = c.String(nullable: false, maxLength: 20),
                    Action = c.Boolean(nullable: false),
                })
                .PrimaryKey(t => t.AclEntryId)
                .ForeignKey("dbo.Topics", t => t.TopicId, cascadeDelete: true)
                .ForeignKey("dbo.Roles", t => t.RoleName, cascadeDelete: true)
                .Index(t => t.TopicId)
                .Index(t => t.RoleName);

            CreateTable(
                "dbo.TopicPosts",
                c => new {
                    TopicPostId = c.Guid(nullable: false),
                    TopicId = c.Int(nullable: false),
                    UserName = c.String(nullable: false, maxLength: 100),
                    DateCreated = c.DateTimeOffset(nullable: false),
                    Text = c.String(nullable: false),
                })
                .PrimaryKey(t => t.TopicPostId)
                .ForeignKey("dbo.Topics", t => t.TopicId, cascadeDelete: true)
                .ForeignKey("dbo.Users", t => t.UserName, cascadeDelete: false)
                .Index(t => t.TopicId)
                .Index(t => t.UserName);

            CreateTable(
                "dbo.RoleMemberships",
                c => new {
                    UserName = c.String(nullable: false, maxLength: 100),
                    RoleName = c.String(nullable: false, maxLength: 100),
                })
                .PrimaryKey(t => new { t.UserName, t.RoleName })
                .ForeignKey("dbo.Users", t => t.UserName, cascadeDelete: true)
                .ForeignKey("dbo.Roles", t => t.RoleName, cascadeDelete: true)
                .Index(t => t.UserName)
                .Index(t => t.RoleName);
        }

        public override void Down() {
            DropIndex("dbo.RoleMemberships", new[] { "RoleName" });
            DropIndex("dbo.RoleMemberships", new[] { "UserName" });
            DropIndex("dbo.TopicPosts", new[] { "UserName" });
            DropIndex("dbo.TopicPosts", new[] { "TopicId" });
            DropIndex("dbo.TopicAclEntries", new[] { "RoleName" });
            DropIndex("dbo.TopicAclEntries", new[] { "TopicId" });
            DropIndex("dbo.Topics", new[] { "TopicCategoryId" });
            DropIndex("dbo.Topics", new[] { "UserName" });
            DropIndex("dbo.Messages", new[] { "ToUserName" });
            DropIndex("dbo.Messages", new[] { "FromUserName" });
            DropIndex("dbo.UserAclEntries", new[] { "RoleName" });
            DropIndex("dbo.UserAclEntries", new[] { "UserName" });
            DropForeignKey("dbo.RoleMemberships", "RoleName", "dbo.Roles");
            DropForeignKey("dbo.RoleMemberships", "UserName", "dbo.Users");
            DropForeignKey("dbo.TopicPosts", "UserName", "dbo.Users");
            DropForeignKey("dbo.TopicPosts", "TopicId", "dbo.Topics");
            DropForeignKey("dbo.TopicAclEntries", "RoleName", "dbo.Roles");
            DropForeignKey("dbo.TopicAclEntries", "TopicId", "dbo.Topics");
            DropForeignKey("dbo.Topics", "TopicCategoryId", "dbo.TopicCategories");
            DropForeignKey("dbo.Topics", "UserName", "dbo.Users");
            DropForeignKey("dbo.Messages", "ToUserName", "dbo.Users");
            DropForeignKey("dbo.Messages", "FromUserName", "dbo.Users");
            DropForeignKey("dbo.UserAclEntries", "RoleName", "dbo.Roles");
            DropForeignKey("dbo.UserAclEntries", "UserName", "dbo.Users");
            DropTable("dbo.RoleMemberships");
            DropTable("dbo.TopicPosts");
            DropTable("dbo.TopicAclEntries");
            DropTable("dbo.Topics");
            DropTable("dbo.TopicCategories");
            DropTable("dbo.Messages");
            DropTable("dbo.UserAclEntries");
            DropTable("dbo.Users");
            DropTable("dbo.Roles");
        }
        
    }
}